package GUIMain;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Point;

import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;

import data.Reference;

public class SplashScreen{
	private static JWindow window;
	private static GridBagConstraints gbc;
	private static boolean initialized = false;
	private static JProgressBar jpb;
	private static Image image;
	private static int progress;
	private static JLabel currentStep;
	
	public void open(){
		initialize();
		window.setVisible(true);
		window.getContentPane().setBackground(Color.LIGHT_GRAY);
		window.getGraphics().drawImage(image,0,0,null);
	}
	public static void close(){
		window.setVisible(false);
		window = null;
		gbc = null;
		initialized = false;
	}
	public void update(int addition,String stepName){
		currentStep.setText("Current Step: " + stepName);
		if(progress + addition >= 100){
			progress = 100;
			jpb.setValue(progress);
			close();
		}else{
			progress+=addition;
			jpb.setValue(progress);
		}
		if(window!=null)
			window.invalidate();
	}
	private void initialize(){
		if(initialized== true)
			return;
		initialized = true;
		progress = 0;
		window = new JWindow();
		gbc = new GridBagConstraints();
		window.setAlwaysOnTop(true);
		window.setLayout(new GridBagLayout());
		jpb = new JProgressBar();
		jpb.setPreferredSize(new Dimension(400,40));
		gbc.insets.left = 0;
		gbc.insets.right = 0;
		gbc.insets.top = 0;
		gbc.insets.bottom = 0;
		currentStep = new JLabel("Loading: ");
		JLabel label = new JLabel("Report Generator");
		Font curFont = label.getFont();

		label.setFont(new Font("ARIAL BLACK", curFont.getStyle(), 45));
		gbc.gridx = 0;
		gbc.gridy = 0;
		window.add(label,gbc);
		gbc.gridy = 1;
		window.add(new JLabel("V. " + Reference.VERSION),gbc);
		gbc.gridx = 0;
		gbc.gridy = 2;
		window.add(currentStep,gbc);
		gbc.gridx = 0;
		gbc.gridy = 3;
		window.add(jpb,gbc);
		
		window.pack();
		window.setLocationRelativeTo(null);
		window.setLocation(new Point(window.getLocation().x, window.getLocation().y-window.getSize().height));
	}

}
